<template>
    <div class="admin">
        <el-card class="!border-none" shadow="never">
            <el-form class="mb-[-16px]" :model="formData" inline>
                <el-form-item label="计量单位名称">
                    <el-input v-model="formData.title" class="w-40" size="small" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage" size="small">查询</el-button>
                    <el-button @click="resetParams" size="small">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card v-loading="pager.loading" class="mt-2 !border-none" shadow="never">
            <el-button v-perms="['unit.unit/add']" type="primary" @click="handleAdd" size="small">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增计量单位
            </el-button>
            <div style="margin-top: 3px;">
                <el-table :data="pager.lists" size="small" :max-height="620" border>
					<el-table-column label="序号" width="55" type="index" align="center"></el-table-column>
					<el-table-column label="名称" prop="title" min-width="100"></el-table-column>
					<el-table-column
					    label="建档时间"
					    prop="create_time"
					    min-width="180"
					></el-table-column>
					<el-table-column
					    label="创建时间"
					    prop="update_time"
					    min-width="180"
					></el-table-column>
                    <el-table-column label="操作" width="120" fixed="right" align="center">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['unit.unit/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-if="row.root != 1"
                                v-perms="['unit.unit/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.nid)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex  justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { adminLists, adminDelete } from '@/api/unit/list'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 表单数据
const formData = reactive({
    title: ''
})
const showEdit = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: adminLists,
    params: formData
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

const handleDelete = async (nid: number) => {
    await feedback.confirm('确定要删除？')
    await adminDelete({ nid })
    getLists()
}
onMounted(() => {
    getLists()
})
</script>
<style scoped lang="scss">
	
</style>